package com.xy.test.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/provider/dept")
public class DeptController {

    @GetMapping("/get/{id}")
    // 当服务出现异常后调用指定的处理方法
    @HystrixCommand(fallbackMethod = "processHystrixGet")
    public String getDept(@PathVariable(value = "id") Integer id) {
        if (id == 0) {
            // 模拟抛出异常的情况
            throw new RuntimeException("该ID： " + id + " 不存在");
        }
        return "dept:" + id;
    }

    /**
     * 预期的处理异常返回数据
     *
     * @return
     */
    public String processHystrixGet(@PathVariable(value = "id") Integer id) {
        return "该编号部门不存在Provider";
    }

    @GetMapping("/list")
    public List<String> getDeptList() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        return list;
    }
}
